#import "Common.h"
#import "SpellSlot.h"

NSString* sSpellEmptyFileName = @"empty.png";

@implementation Spell

@synthesize sType=mType;

-(int)sell
{
	assert(mInfo);
	return mInfo->sell;
}

-(float)duration
{
	assert(mInfo);
	return mInfo->duration;
}

-(NSString*)spellName
{
	assert(mInfo);
	return NSLocalizedString(mInfo->name,nil);
}

-(NSString*)desc
{
	assert(mInfo);
	return NSLocalizedString(mInfo->description,nil);
}

-(NSString*)fileName
{
	assert(mInfo);
	return mInfo->fileName;
}

-(NSString*)durationStr
{
	return [NSString stringWithFormat:@"%@ %d%@",NSLocalizedString(@"SpellDuration",nil),(int)mInfo->duration,
	 NSLocalizedString(@"SpellDurationSuffix",nil)];
}

-(id)initWithType:(SpellType)type trans:(bool)trans
{
	if ((self = [super init])) {
		mIcon = NULL;
		mTrans = trans;
		[self change:type];
		[self autorelease];
	}
	return self;
}

-(bool)checkClick:(UITouch*)touch
{
	CGPoint pos = [self convertTouchToNodeSpace:touch];
	CGSize sizeSpell = [mIcon contentSize];
	CGRect rc = CGRectMake(-sizeSpell.width/2,-sizeSpell.height/2,sizeSpell.width,sizeSpell.height);
	
	return CGRectContainsPoint(rc, pos);
}

-(void)change:(SpellType)type
{
	mCasted = false;
	mType = type;

	if(mIcon)
		[self removeChild:mIcon cleanup:YES];

	if(type == ST_None) {
		mInfo = NULL;
		mIcon = [CCSprite spriteWithSpriteFrameName:sSpellEmptyFileName];
	} else {
		mInfo = &gSpellData[type];
		mIcon = [CCSprite spriteWithSpriteFrameName:mInfo->fileName];
	}
	
	if(mTrans)
		mIcon.opacity = 150;
	else
		mIcon.opacity = 255;

	[self addChild:mIcon];
}

-(void)cast
{
	if( mType != ST_None && !mCasted)	{
		mCasted = true;
		self.scale = 1.3;

		[self runAction:[CCSequence actions:
			[CCScaleTo actionWithDuration:.1f scale:0.9f],
			[CCScaleTo actionWithDuration:.1f scale:1.1f],
			[CCScaleTo actionWithDuration:.1f scale:1.0f],
			[CCCallFunc actionWithTarget:self selector:@selector(castFinish)], NULL]];
	}
}

-(void) castFinish
{
	[self change:ST_None];
}

@end

@implementation SpellSlot

-(id)init
{
	if ((self = [super init])) {
		[[CCTextureCache sharedTextureCache] addImage:[NSString stringWithFormat:@"spells%@.png", gHDSuffix]];
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"spells%@.plist", gHDSuffix]];

		for(int i=0; i<SPELLSLOT_COUNT; i++) {
			mSpells[i] = [[Spell alloc] initWithType:gGlobalData.spells[i] trans:true];
			mSpells[i].position = ccp(0,-60*(i-1));
			[self addChild:mSpells[i]];
		}

		if(gSettingData.spellInRight)
			self.position = ccp(450,160);
		else
			self.position = ccp(30,160);
	}
	return self;
}

-(int)checkClick:(UITouch*)touch
{
	for(int i=0; i<SPELLSLOT_COUNT; i++) {
		if([mSpells[i] checkClick:touch])
			return i;
	}
	
	return -1;
}

-(void)cast:(int)idx
{
	[mSpells[idx] cast];
}

-(void)changeSpell:(int)idx type:(SpellType)type
{
	[mSpells[idx] change:type];
}

-(CGPoint)spellPos:(int)idx
{
	return ccpAdd(self.position, mSpells[idx].position);
}

-(Spell*)spell:(int)idx
{
	return mSpells[idx];
}

@end
